package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.ClubActivitiesMapper;
import com.ruoyi.system.domain.ClubActivities;
import com.ruoyi.system.service.IClubActivitiesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 社团活动Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-25
 */
@Service
public class ClubActivitiesServiceImpl implements IClubActivitiesService {
    @Autowired
    private ClubActivitiesMapper clubActivitiesMapper;

    /**
     * 查询社团活动
     *
     * @param activityId 活动主键
     * @return 社团活动
     */
    @Override
    public ClubActivities selectClubActivitiesByActivityId(Long activityId) {
        return clubActivitiesMapper.selectClubActivitiesByActivityId(activityId);
    }

    /**
     * 查询社团活动列表
     *
     * @param clubActivities 社团活动
     * @return 社团活动
     */
    @Override
    public List<ClubActivities> selectClubActivitiesList(ClubActivities clubActivities) {
        Long userId = SecurityUtils.getUserId();
        // 判断是否为管理员
        boolean isAdmin = SecurityUtils.isAdmin(userId);
        // 优化后的查询，一次性获取所有需要的数据，避免N+1查询问题
        return clubActivitiesMapper.selectClubActivitiesList(clubActivities, userId, isAdmin);
    }

    /**
     * 根据社团ID查询活动列表
     *
     * @param clubId 社团ID
     * @return 社团活动
     */
    @Override
    public List<ClubActivities> selectClubActivitiesByClubId(Long clubId) {
        return clubActivitiesMapper.selectClubActivitiesByClubId(clubId);
    }

    /**
     * 查询即将开始的活动
     *
     * @param limit 限制数量
     * @return 社团活动
     */
    @Override
    public List<ClubActivities> selectUpcomingActivities(Integer limit) {
        return clubActivitiesMapper.selectUpcomingActivities(limit);
    }

    /**
     * 查询公开的活动列表（无权限控制）
     */
    @Override
    public List<ClubActivities> selectPublicActivitiesList(ClubActivities clubActivities) {
        // 不进行权限控制，直接查询所有公开活动
        return clubActivitiesMapper.selectPublicActivitiesList(clubActivities);
    }

    /**
     * 查询用户参与的活动
     *
     * @param userId 用户ID
     * @return 社团活动
     */
    @Override
    public List<ClubActivities> selectUserParticipatedActivities(Long userId) {
        return clubActivitiesMapper.selectUserParticipatedActivities(userId);
    }

    /**
     * 新增社团活动
     *
     * @param clubActivities 社团活动
     * @return 结果
     */
    @Override
    public int insertClubActivities(ClubActivities clubActivities) {
        return clubActivitiesMapper.insertClubActivities(clubActivities);
    }

    /**
     * 修改社团活动
     *
     * @param clubActivities 社团活动
     * @return 结果
     */
    @Override
    public int updateClubActivities(ClubActivities clubActivities) {
        return clubActivitiesMapper.updateClubActivities(clubActivities);
    }

    /**
     * 批量删除社团活动
     *
     * @param activityIds 需要删除的活动主键
     * @return 结果
     */
    @Override
    public int deleteClubActivitiesByActivityIds(Long[] activityIds) {
        return clubActivitiesMapper.deleteClubActivitiesByActivityIds(activityIds);
    }

    /**
     * 删除社团活动信息
     *
     * @param activityId 活动主键
     * @return 结果
     */
    @Override
    public int deleteClubActivitiesByActivityId(Long activityId) {
        return clubActivitiesMapper.deleteClubActivitiesByActivityId(activityId);
    }
}
